const express = require('express')
const fs = require('fs')

// 导出模型
let api = require('./student.js')

// 创建数据
// api.create([
//             { name: '张飞', gender: 0, hobbies: '干架', level: '一般' },
//             { name: '赵云', gender: 0, hobbies: '随便', level: '良好' },
//             { name: '关羽', gender: 0, hobbies: '阿萨德', level: '一般' },
//             { name: '马超', gender: 0, hobbies: '干阿萨德架', level: '一般' },
//             { name: '黄忠', gender: 0, hobbies: '干SaaS架', level: '一般' },
//             { name: '貂蝉', gender: 1, hobbies: '跳舞', level: 'good' }
//         ],
//         function(err, ret) {
//             if (err) {
//                 console.log('数据插入成功');
//             } else {
//                 console.log('数据创建成功');
//                 // console.log(ret);
//             }
//         })
//  创建路由容器
let router = express.Router()
    // 挂载路由
router.get('/', function(req, res) {
    // console.log(11)
    api.find(function(err, students) {
        if (err) {
            console.log('数据查询失败');
        } else {
            res.render('index.html', {
                students: students
            })
        }
    })


})

// 
router.get('/students/new', function(req, res) {
    res.render('new.html')
})

// 修改页面
router.get('/students/update', function(req, res) {

    let StudentId = req.query.id
    api.findById(StudentId, function(err, ret) {
        if (err) {
            console.log('查询失败');
        } else {
            console.log('查询成功');
            res.render('update.html', {
                student: ret
            })
        }
    })
})


// 更新提交
router.post('/student/update', function(req, res) {
    let AfterupdatedStudent = req.body
    let studentId = AfterupdatedStudent.id
    api.findByIdAndUpdate(studentId, AfterupdatedStudent, function(err, ret) {
        if (err) {
            res.status(500).send('Update Error')
        } else {
            res.redirect('/')
        }
    })

})

// 新增提交
router.post('/', function(req, res) {

    let singleSudent = req.body
    api.create(singleSudent, function(err, ret) {
        if (err) {
            res.status(500).send('Create Error')

        } else {
            res.redirect('/')
        }
    })

})


// 更新提交

router.post('/students/update', function(req, res) {
    api.updateById(req.body, function(err) {
        if (err) {
            res.status(500).send('Server error')
        } else {
            res.redirect('/')
        }
    })
})



// 查询页面
router.get('/students/search', function(req, res) {
    res.render('search.html')
})

// 查询
router.post('/student/search', function(req, res) {
    let studentId = req.body.id
    api.find(function(err, students) {
        if (err) {
            console.log('查询失败');
        } else {
            let findedStudent = students[studentId - 1]
            res.render('searchDetail.html', {
                student: findedStudent
            })
        }
    })
})

// 删除页面
router.get('/students/delete', function(req, res) {
        let studentId = req.query.id
        api.findById(studentId, function(err, student) {
            if (err) {
                console.log('查询失败');

            } else {
                res.render('delete.html', {
                    student: student
                })
            }
        })
    })
    // 删除路由

router.post('/student/delete/sure', function(req, res) {
    let studentId = req.body.id
    api.findByIdAndDelete(studentId, function(err, ret) {
        if (err) {
            res.status(500).send('Delete Failed')
        } else {
            res.redirect('/')
        }
    })
})

module.exports = router